package com.swz.dcrm.ui.car;


import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.swz.dcrm.adpter.LatelySearchCarModelAdapter;
import com.swz.dcrm.db.TbCarModel;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.CarBrand;
import com.swz.dcrm.model.CarModel;

import com.swz.dcrm.model.CarSeries;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.swz.dcrm.ui.viewmodel.CarBrandViewModel;
import com.swz.dcrm.ui.viewmodel.CarManagementViewModel;
import com.swz.dcrm.util.CustomDecoration;
import com.swz.dcrm.util.Tool;
import com.xh.baselibrary.widget.BounceScrollView;
import com.xh.baselibrary.widget.ClickImageView;
import com.yanzhenjie.recyclerview.OnItemMenuClickListener;
import com.yanzhenjie.recyclerview.SwipeMenuItem;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.util.List;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class BuyCarCalFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.toolbar)
    ConstraintLayout toolbar;
    @BindView(R.id.scroll)
    BounceScrollView scrollView;
    @BindView(R.id.tv_buy_car_cal)
    TextView tvBuyCarCal;
    @BindView(R.id.iv_back)
    ClickImageView ivBack;
    @BindView(R.id.tv_brand)
    TextView tvBrand;
    @BindView(R.id.tv_model)
    TextView tvModel;
    @BindView(R.id.rv)
    SwipeRecyclerView swipeRecyclerView;
    @BindView(R.id.c_lately_search)
    ConstraintLayout cLatelySearch;

    private LatelySearchCarModelAdapter latelySearchCarModelAdapter;

    private List<TbCarModel> tbCarModelList;

    @Inject
    CarBrandViewModel carBrandViewModel;


    public static BuyCarCalFragment newInstance() {

        return new BuyCarCalFragment();
        // Required empty public constructor
    }

    @Override
    public void onLoadRetry() {
        carBrandViewModel.getLatelySearch().observe(this, tbCarModels -> {
            tbCarModelList=tbCarModels;
            if(tbCarModels!=null&&tbCarModels.size()>0){
                cLatelySearch.setVisibility(View.VISIBLE);
                latelySearchCarModelAdapter=new LatelySearchCarModelAdapter(getContext(),tbCarModels);
                swipeRecyclerView.setAdapter(latelySearchCarModelAdapter);
            }else {
                cLatelySearch.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_buy_car_cal;
    }

    @Override
    public boolean initView() {

        getDigger().inject(this);
        ConstraintLayout.LayoutParams layoutParams= (ConstraintLayout.LayoutParams) toolbar.getLayoutParams();
        layoutParams.height=getStatusBarHeight(getContext())+Tool.dip2px(getActivity(),50);
        toolbar.setLayoutParams(layoutParams);
        toolbar.setPadding(0,getStatusBarHeight(getContext()),0,0);
        toolbar.setBackgroundColor(Color.TRANSPARENT);
        ivBack.setImageDrawable(getResources().getDrawable(R.mipmap.back_white));
        title.setText(getString(R.string.buy_car_cal_title));
        title.setVisibility(View.GONE);
        scrollView.setOnScrollChangeListener((BounceScrollView.OnScrollChangeListener) (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
            if(scrollY<=tvBuyCarCal.getTop()){
                title.setVisibility(View.GONE);
                ivBack.setImageDrawable(getResources().getDrawable(R.mipmap.back_white));
                toolbar.setBackgroundColor(Color.TRANSPARENT);
            }else {
                title.setVisibility(View.VISIBLE);
                ivBack.setImageDrawable(getResources().getDrawable(R.mipmap.back));
                toolbar.setBackgroundColor(getResources().getColor(R.color.white));
            }
        });

        carBrandViewModel.getSelectedCarSeries().observe(this,carSeries -> {
            if(carSeries!=null){

                tvBrand.setText(carBrandViewModel.getSelectedCarBrand().getValue().getName()+"-"+ carBrandViewModel.getSelectedCarSeries().getValue().getName());
            }
        });

        carBrandViewModel.getSelectedCarBrand().observe(this, carBrand ->{
            if(carBrand!=null){
                tvBrand.setText(carBrand.getName());
            }


        }) ;
        carBrandViewModel.getSelectedCarModel().observe(this, carModel -> {
           if(carModel!=null){
               tvModel.setText(carBrandViewModel.getSelectedCarModel().getValue().getName());
           }
        });

        swipeRecyclerView.addItemDecoration(new CustomDecoration(getContext(),0,0,15,15));

        swipeRecyclerView.setSwipeMenuCreator((swipeLeftMenu, swipeRightMenu, viewType) -> {

            int width = Tool.dip2px(getActivity(),70);
            SwipeMenuItem deleteItem = new SwipeMenuItem(getContext());
            deleteItem.setText(getString(R.string.buy_car_cal_del))
                    .setTextColor(Color.WHITE)
                    .setBackgroundColor(getResources().getColor(R.color.blue_0f6eff))
                    .setHeight(ViewGroup.LayoutParams.MATCH_PARENT)
                    .setWidth(width);
           swipeRightMenu.addMenuItem(deleteItem); // 在Item右侧添加一个菜单。
        });
        swipeRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        swipeRecyclerView.setOnItemMenuClickListener(mItemMenuClickListener);
        swipeRecyclerView.setOnItemClickListener((view, adapterPosition) -> {
            TbCarModel tbCarModel=tbCarModelList.get(adapterPosition);
            CarBrand carBrand=new CarBrand();
            carBrand.setId(tbCarModel.getCarBrandId());
            carBrand.setName(tbCarModel.getCarBrandDesc());
            CarSeries carSeries=new CarSeries();
            carSeries.setBrandId(carBrand.getId());
            carSeries.setId(tbCarModel.getCarSeriesId());
            carSeries.setName(tbCarModel.getCarSeriesDesc());
            CarModel carModel=new CarModel();
            carModel.setId(tbCarModel.getCarModelId());
            carModel.setSeriesId(carSeries.getId());
            carModel.setName(tbCarModel.getCarModelDesc());
            carBrandViewModel.getSelectedCarBrand().setValue(carBrand);
            carBrandViewModel.getSelectedCarSeries().setValue(carSeries);
            carBrandViewModel.getSelectedCarModel().setValue(carModel);
        });
        onLoadRetry();
        return false;
    }


    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }
    @OnClick(R.id.tv_cal)
    public void cal(){
        if(carBrandViewModel.getSelectedCarBrand().getValue()!=null&&
            carBrandViewModel.getSelectedCarSeries().getValue()!=null
            &&carBrandViewModel.getSelectedCarModel().getValue()!=null){
            TbCarModel tbCarModel=new TbCarModel();
            tbCarModel.setCarBrandId(carBrandViewModel.getSelectedCarBrand().getValue().getId());
            tbCarModel.setCarBrandDesc(carBrandViewModel.getSelectedCarBrand().getValue().getName());
            tbCarModel.setCarModelId(carBrandViewModel.getSelectedCarModel().getValue().getId());
            tbCarModel.setCarModelDesc(carBrandViewModel.getSelectedCarModel().getValue().getName());
            tbCarModel.setCarSeriesId(carBrandViewModel.getSelectedCarSeries().getValue().getId());
            tbCarModel.setCarSeriesDesc(carBrandViewModel.getSelectedCarSeries().getValue().getName());
            carBrandViewModel.insertSearch(tbCarModel);
            go(null,R.id.action_buyCarCalFragment_to_feeInTotalFragment,null);
        }else {
            showToast("请先选择车型");
        }

    }
    @OnClick(R.id.tv_brand)
    public void selectBrand(){
        go(null,R.id.action_buyCarCalFragment_to_carBrandFragment,null);
    }
    @OnClick(R.id.tv_clear)
    public void clear(){
        carBrandViewModel.delAllLatelySearch(tbCarModelList);
    }

    @OnClick(R.id.tv_model)
    public void selectModel(){
        if(carBrandViewModel.getSelectedCarBrand().getValue()!=null){
            CarBrandFragmentArgs.Builder builder=new CarBrandFragmentArgs.Builder();
            builder.setBrand(new Gson().toJson(carBrandViewModel.getSelectedCarBrand().getValue()));
            go(null,R.id.action_buyCarCalFragment_to_carBrandFragment,builder.build().toBundle());
        }else {
            showToast("请先选择车型");
        }
    }
    OnItemMenuClickListener mItemMenuClickListener = (menuBridge, position) -> {
        // 任何操作必须先关闭菜单，否则可能出现Item菜单打开状态错乱。
        menuBridge.closeMenu();
        // 左侧还是右侧菜单：
        int direction = menuBridge.getDirection();
        // 菜单在Item中的Position：
        int menuPosition = menuBridge.getPosition();
        if(tbCarModelList!=null&&position<=tbCarModelList.size()){
            carBrandViewModel.delLatelySearchById( tbCarModelList.get(position));
        }
    };

}
